package cn.silver.module.authority.service.impl;

import cn.silver.framework.mybatis.service.impl.BaseServiceImpl;
import cn.silver.module.authority.domain.SysRoleMenu;
import cn.silver.module.authority.mapper.SysRoleMenuMapper;
import cn.silver.module.authority.bean.RoleBean;
import cn.silver.module.authority.bean.RoleMenuBean;
import cn.silver.module.authority.param.RoleParam;
import cn.silver.module.system.param.MenuParam;
import cn.silver.module.authority.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenuMapper, SysRoleMenu, RoleParam, RoleMenuBean> implements ISysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void save(RoleBean model) {
        if (CollectionUtils.isNotEmpty(model.getMenuIds())) {
            this.baseMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, model.getId()));
            model.getMenuIds().stream().forEach(item -> {
                SysRoleMenu cur = new SysRoleMenu(model.getId(), item);
                cur.preInsert();
                this.baseMapper.insert(cur);
            });
        }
    }
}
